package java6703.lesson07;

import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import java.io.File;
import java.text.DecimalFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

//控制器类
public class ListFile6703Controller {
    @FXML
    private ListView<String> lvFiles;

    @FXML
    private TextField tfDir;

    @FXML
    private Label lblCount;

    @FXML
    void listAll(ActionEvent event) {

    }

    @FXML
    void initialize() {
        tfDir.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                ObservableList<String> items = lvFiles.getItems();
                items.clear();
                int dirCount = 0;
                int fileCount = 0;
                String inDir = tfDir.getText().trim();
                File file;
                if ("".equals(inDir)) {
                    file = new File(".");
                } else {
                    file = new File(inDir);
                }
                File[] files = file.listFiles();

                //将File[]转成List<>进行排序
                List<File> fileList = Arrays.asList(files);
                Collections.sort(fileList, new Comparator<File>() {
                    @Override
                    public int compare(File o1, File o2) {
                        if (o1.isDirectory() && o2.isFile()) {
                            return -1;
                        }
                        if (o1.isFile() && o2.isDirectory()) {
                            return 1;
                        }
                        return o1.getName().compareTo(o2.getName());
                    }
                });

                for (File f : files) {

                    //获取文件的修改时间进行格式化
                    long l = f.lastModified();
                    Instant instant = Instant.ofEpochMilli(l);
                    String date = LocalDateTime
                            .ofInstant(instant, ZoneId.systemDefault())
                            .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

                    //截取目录或文件名
                    String fileName = f.toString()
                            .substring(f.toString().lastIndexOf("\\") + 1);
                    if (f.isDirectory()) {
                        items.add("文件夹, " + fileName + ", " + date);
                        dirCount++;
                    }
                    if (f.isFile() && f.exists()) {
                        String fileSize = new DecimalFormat("#")
                                .format(Math.ceil((double) f.length() / 1024));
                        items.add(fileName + ", " + date + ", " + fileSize + "KB");
                        fileCount++;
                    }
                }
                lblCount.setText("子目录：" + dirCount + ", 文件：" + fileCount);
            }
        });
    }
}
